package com.woniuxy.exam.service.impl;

import com.woniuxy.commons.entity.PaperClassification;
import com.woniuxy.exam.mapper.PaperClassificationMapper;
import com.woniuxy.exam.service.PaperClassificationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author 余枭春
 * @Date 2020/7/18 15:00
 * @Description:
 **/
@Service
public class PaperClassificationServiceImpl implements PaperClassificationService {
    private static final int STATUS_USED = 1;
    private static final int STATUS_NOTUSED = 2;

    @Autowired
    private PaperClassificationMapper paperClassificationMapper;

    @Override
    public List<PaperClassification> allPaperClassification() {
        return paperClassificationMapper.allPaperClassification(STATUS_USED);
    }

    @Override
    public int insertPaperClassification(PaperClassification paperClassification) {
        //先看是否存在同名的分类
        PaperClassification classification = paperClassificationMapper.findPaperClassificationByName(paperClassification.getName());
        //如果有
        if (classification != null) {
            //如果不可用，就先转换状态，再返回相应id
            if (classification.getStatus() != STATUS_NOTUSED) {
                classification.setStatus(STATUS_USED);
                int res = paperClassificationMapper.updatePaperClassificationStatusById(classification);
                if(res>0){
                    return classification.getId();
                }
            }else{
                //如果在用，就返回相应的id表示已经存在同名的分类
                return classification.getId();
            }
        }else {
            //如果没有，就新建一条
           return paperClassificationMapper.insertPaperClassification(paperClassification);
        }
        return 0;
    }

    @Override
    public int updataPaperClassificationNameById(PaperClassification paperClassification) {
        return paperClassificationMapper.updataPaperClassificationNameById(paperClassification);
    }

    @Override
    public int deletePaperClassificationById(int id) {
        PaperClassification paperClassification=paperClassificationMapper.findPaperClassificationById(id);
        if (paperClassification!=null){
            paperClassification.setStatus(STATUS_NOTUSED);
            return paperClassificationMapper.updatePaperClassificationStatusById(paperClassification);
        }else{
            //原id不存在返回-1
            return -1;
        }
    }

    @Override
    public int updatePaperClassificationStatusById(PaperClassification paperClassification) {
        return paperClassificationMapper.updatePaperClassificationStatusById(paperClassification);
    }
}
